package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.CompanySizeAdapter;

import java.util.ArrayList;
import java.util.List;

public class CompanySizePoppupWindow extends PopupWindow {
    private TextView tv_title;
    private Context context;
    private View layout;
    private RecyclerView rc_company_size;
    private ImageView iv_dismiss;
    private String companyName;
    private CompanySizeAdapter companySizeAdapter;

    public OnHozonClickListener getOnHozonClickListener() {
        return onHozonClickListener;
    }

    public void setOnHozonClickListener(OnHozonClickListener onHozonClickListener) {
        this.onHozonClickListener = onHozonClickListener;
    }

    private OnHozonClickListener onHozonClickListener;

    public static CompanySizePoppupWindow getInstance(Context context) {
        return new CompanySizePoppupWindow(context);
    }

    public static CompanySizePoppupWindow getInstance(Context context, String companyName) {
        return new CompanySizePoppupWindow(context, companyName);
    }

    private CompanySizePoppupWindow(Context context) {
        super(context);
        this.context = context;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }

    private CompanySizePoppupWindow(Context context, String companyName) {
        super(context);
        this.context = context;
        this.companyName = companyName;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_company_size, null);


        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        tv_title = layout.findViewById(R.id.tv_title);
        rc_company_size = layout.findViewById(R.id.rc_company_size);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rc_company_size.setLayoutManager(layoutManager);
        companySizeAdapter = new CompanySizeAdapter(context);
        rc_company_size.setAdapter(companySizeAdapter);

        List<String> stringList = new ArrayList<>();
        stringList.add("60人以下");
        stringList.add("60-100人");
        stringList.add("100-300人");
        companySizeAdapter.setNewInstance(stringList);
//        stringList.add("300-1000人(待开放)");
//        stringList.add("1000-2000人(待开放)");
//        stringList.add("2000人以上(待开放)");
        companySizeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (onHozonClickListener != null) {
                    onHozonClickListener.onHozomClick(companySizeAdapter.getData().get(position));
                }
            }
        });
    }


    private void initEvent() {

        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface OnHozonClickListener {
        void onHozomClick(String corpsName);
    }
}
